package com.trw.trp.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import com.trw.trp.bean.ModelDetail;
import com.trw.trp.bean.ModelDetail.Field;
import com.trw.trp.bean.ModelDetail.ModelSection;

public class ModelUtil {
	public static Map<Long, ModelDetail.Field> getAllFields(ModelDetail detail) {
		Map<Long, ModelDetail.Field> fields = new HashMap<Long, ModelDetail.Field>();
		if (detail != null && detail.getSectionList() != null) {
			for (ModelSection section : detail.getSectionList()) {
				if (section.getFieldList() != null) {
					for (Field field : section.getFieldList()) {
						fields.put(field.getFieldId(), field);
					}
				}
			}
		}
		return fields;
	}

	public static List<JSONObject> parseMatrix(ModelDetail.Field field) {
		List<JSONObject> ret = new ArrayList<JSONObject>();
		if (field != null) {
			String val = field.getSelectedValue();
			JSONArray array = JSONArray.fromObject("[" + val + "]");
			for (int i = 0; i < array.size(); i++) {
				try {
					JSONObject jsonObj = array.getJSONObject(i);
					ret.add(jsonObj);
				} catch (JSONException e) {
				}
			}
		}
		return ret;
	}

}
